
/*
 * Desktop_SearchItem.java
 * Created on Mar 31, 2011, 7:08:34 PM
 */
package DesktopGUIpackage;

import Cart.Cart;
import Controller.DesktopController;
import Controller.DesktopControllerHelper;
import DTO.Category;
import DTO.Item;
import Session.SessionManager;
import Utilities.MoneyFormatter;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.table.TableColumn;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

/**
 *
 * @author edaype and tajinder
 */
public class Desktop_SearchItem extends javax.swing.JPanel implements TreeSelectionListener {

    private DesktopController desktopController = null;
    private DefaultMutableTreeNode selectedNode = null;
    private Object[][] obj = null;
    private BufferedImage image = null;
    private Category selectedCategory = null;
    private List<Item> searchResultItems = null;
    private DesktopControllerHelper desktopControllerHelper = null;
    private javax.swing.JLabel ErrorLabel;
    private MoneyFormatter moneyFormatter;
    private ArrayList<Item> searchResults;
    private SessionManager sessionManager;
    private Cart cart;

    /** Creates new form Desktop_SearchItem
     * @param desktopController
     */
    public Desktop_SearchItem(DesktopControllerHelper desktopControllerHelper) {
        initComponents();
        this.desktopControllerHelper = desktopControllerHelper;
        desktopController = desktopControllerHelper.getDesktopController();
        searchResultItems = new ArrayList<Item>();
        moneyFormatter = new MoneyFormatter();
        sessionManager = desktopController.getSessionMgr();
        cart = sessionManager.getCart();
        searchResults = cart.getSearchResults();
        customInit();
    }

    /**
     *
     * @param e
     */
    public void valueChanged(TreeSelectionEvent e) {

        System.out.println("listener invoked");
        ErrorMessageLabel.setVisible(false);
        selectedNode = (DefaultMutableTreeNode) CategoryTree.getLastSelectedPathComponent();
        // Category parentCategory;
        selectedCategory = (Category) selectedNode.getUserObject();
        if (selectedCategory.getCtid() != 0) {
            DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode) selectedNode.getParent();
            if (parentNode != null) {
                Category parentCategory = (Category) parentNode.getUserObject();
            }
            desktopController.searchByCategory(selectedCategory.getCtid());
            System.out.println("search category = " + selectedCategory.getCtid());
        }

        System.out.println("selected node " + selectedNode);
        System.out.println("selected category " + selectedNode.getUserObject());

        updateModel();
    }

    public void customInit() {
        ErrorMessageLabel.setVisible(false);
        OptionSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[]{"Name", "Make", "Model", "Description"}));
        List<Category> list = desktopController.getCategories();
        System.out.println(" root category is " + list.get(0));
        System.out.println(" catgories in db " + list.size());
        DefaultMutableTreeNode root = makeTree(list, list.get(0));
        CategoryTree = new JTree(root);
        //Where the tree is initialized:
        CategoryTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        //Listen for when the selection changes.
        CategoryTree.addTreeSelectionListener(this);
        CategoryTree.setRootVisible(false);
        CategoryPane.setViewportView(CategoryTree);
        updateModel();
    }

    /**
     *
     * @param list
     * @param rootNode
     * @return
     */
    private DefaultMutableTreeNode makeTree(List<Category> list, Category rootNode) {

        DefaultMutableTreeNode node = new DefaultMutableTreeNode(rootNode);
        DefaultMutableTreeNode child;
        for (int i = 0; i < list.size(); i++) {
            Category nodeSpecifier = list.get(i);
            // System.out.println(" node specifier id" + nodeSpecifier.getPctid());
            if (nodeSpecifier.getPctid() == rootNode.getCtid()) {
                child = makeTree(list, nodeSpecifier);
                node.add(child);
            }
        }
        return node;
    }

    public void updateModel() {
        searchResults = (ArrayList<Item>) cart.getSearchResults().clone();
        System.out.println("search results size = " + searchResults.size());
        BufferedImage image = null;

        Object[][] obj = new Object[searchResults.size()][5];
        ListIterator<Item> iter = searchResults.listIterator();
        int count = 0;
        System.out.println(searchResults.size());
        if (searchResults.size() > 0) {
            while (iter.hasNext()) {
                Item temp = iter.next();
                try {
                    String location = System.getProperty("user.dir");
                    location += "/src/DesktopGUIpackage/images/" + temp.getImgURL();
                    File imageFile = new File(location);
                    image = ImageIO.read(imageFile);
                } catch (IOException e) {
                }
                BufferedImage resizedImage = resize(image, 100, 100);
                ImageIcon icon = new ImageIcon(resizedImage);

                obj[count][0] = icon;
                obj[count][1] = temp.getName();
                obj[count][2] = temp.getMake();
                obj[count][3] = temp.getModel();
                obj[count][4] = new DecimalFormat("##,###.00").format(temp.getRetailPrice());
                count++;
            }
        }

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                obj,
                new String[]{
                    "Image", "Name", "Make", "Model", "Price"
                }) {

            boolean[] canEdit = new boolean[]{
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit[columnIndex];
            }

            public Class getColumnClass(int columnIndex) {
                return columnIndex == 0 ? ImageIcon.class : Object.class;
            }
        });

        TableColumn column = null;
        column = jTable1.getColumnModel().getColumn(0);
        column.setPreferredWidth(100); //third column is bigger
        column = jTable1.getColumnModel().getColumn(1);
        column.setPreferredWidth(325);
        column = jTable1.getColumnModel().getColumn(2);
        column.setPreferredWidth(100);
        column = jTable1.getColumnModel().getColumn(3);
        column.setPreferredWidth(75);
        column = jTable1.getColumnModel().getColumn(4);
        column.setPreferredWidth(100);

        jTable1.setRowHeight(100);
        jTable1.setColumnSelectionAllowed(false);
        jTable1.setRowSelectionAllowed(true);
    }

    private BufferedImage resize(BufferedImage image, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(image, 0, 0, width, height, null);
        g.dispose();
        return resizedImage;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TopPanel = new javax.swing.JPanel();
        SearchLabel = new javax.swing.JLabel();
        SearchField = new javax.swing.JTextField();
        OptionSearch = new javax.swing.JComboBox();
        GoButton = new javax.swing.JButton();
        ByLabel = new javax.swing.JLabel();
        CategoryPane = new javax.swing.JScrollPane();
        CategoryTree = new javax.swing.JTree();
        ButtonPanel = new javax.swing.JPanel();
        SearchTitle = new javax.swing.JLabel();
        DetailsButton1 = new javax.swing.JButton();
        ErrorMessageLabel = new javax.swing.JLabel();
        InstructionsLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));

        TopPanel.setBackground(new java.awt.Color(255, 255, 255));
        TopPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        SearchLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        SearchLabel.setText("Search");

        SearchField.setFont(new java.awt.Font("Tahoma", 0, 24));

        OptionSearch.setEditable(true);
        OptionSearch.setFont(new java.awt.Font("Tahoma", 0, 24));
        OptionSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Name", "Price", "Make", "Model", "Description" }));

        GoButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        GoButton.setText("Go");
        GoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GoButtonActionPerformed(evt);
            }
        });

        ByLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        ByLabel.setText(" By");

        javax.swing.GroupLayout TopPanelLayout = new javax.swing.GroupLayout(TopPanel);
        TopPanel.setLayout(TopPanelLayout);
        TopPanelLayout.setHorizontalGroup(
            TopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TopPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SearchLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SearchField, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addComponent(ByLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(OptionSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 152, Short.MAX_VALUE)
                .addComponent(GoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        TopPanelLayout.setVerticalGroup(
            TopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TopPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SearchLabel)
                    .addComponent(SearchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(GoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ByLabel)
                    .addComponent(OptionSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        CategoryTree.setFont(new java.awt.Font("Tahoma", 0, 16));
        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("Categories at DigiStore");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Cameras & Camcorders");
        javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Brand");
        javax.swing.tree.DefaultMutableTreeNode treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Sony");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Nikon");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Canon");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Lumix");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Price");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Under $99");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$100 - $199");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$200 - $299");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$300 - $599");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$Over $600");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Cell Phones");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Carrier");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Fido");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Bell");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Rogers");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Koodoo");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Mobile City");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Virgin Mobile");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Price");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Under $99");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$100 - $199");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$200 - $299");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$Over $300");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Computers");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Desktop");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Brand");
        javax.swing.tree.DefaultMutableTreeNode treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Acer");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Asus");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Apple");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Dell");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("HP");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Toshiba");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Sony");
        treeNode4.add(treeNode5);
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Price");
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Under $999");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("$1000 - $1999");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("$2000 - $2999");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("$Over $3000");
        treeNode4.add(treeNode5);
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Laptop");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Brand");
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Acer");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Asus");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Apple");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Lenovo");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Dell");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("HP");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Toshiba");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Sony");
        treeNode4.add(treeNode5);
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Price");
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Under $999");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("$1000 - $1999");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("$2000 - $2999");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("$Over $3000");
        treeNode4.add(treeNode5);
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Accessories");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Cables");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Mice");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Keyboards");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Printers");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Routers");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Storage");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("Gaming");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("PlayStation");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Accessories");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Games");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Wii");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Accessories");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Games");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("XBOX");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Accessories");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Games");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("GPS/Car Electronics");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Brand");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Garmin");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("TomTom");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Insignia");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Price");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Under $99");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$100 - $199");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$200 - $299");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$Over $300");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("TV & Home Theater");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("TV Type");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("3D LED");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("LED");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Plasma");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("LCD");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Projectors");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("TV Brands");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Sony");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("LG");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Panasonic");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Toshiba");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Sharp");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("TV Sizes");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Under 30\"");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("32\" - 39\"");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("40\" - 49\"");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("50\" - 59\"");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Over 60\"");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Price");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Under $299");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$300 - $599");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$600 - $999");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$1000 - $1999");
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("$Over $2000");
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("Home Theater");
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Players");
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Bluray");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("DVD");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("7.1 Audio");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("5.1 Audio");
        treeNode4.add(treeNode5);
        treeNode3.add(treeNode4);
        treeNode4 = new javax.swing.tree.DefaultMutableTreeNode("Price");
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("Under $99");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("$100 - $199");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("$200 - $299");
        treeNode4.add(treeNode5);
        treeNode5 = new javax.swing.tree.DefaultMutableTreeNode("$Over $300");
        treeNode4.add(treeNode5);
        treeNode3.add(treeNode4);
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        CategoryTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        CategoryPane.setViewportView(CategoryTree);

        ButtonPanel.setBackground(new java.awt.Color(255, 255, 255));

        SearchTitle.setFont(new java.awt.Font("Copperplate Gothic Bold", 3, 48));
        SearchTitle.setText("Search Item");

        DetailsButton1.setFont(new java.awt.Font("Tahoma", 0, 18));
        DetailsButton1.setText("View Details");
        DetailsButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DetailsButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
        ButtonPanel.setLayout(ButtonPanelLayout);
        ButtonPanelLayout.setHorizontalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonPanelLayout.createSequentialGroup()
                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, ButtonPanelLayout.createSequentialGroup()
                        .addGap(79, 79, 79)
                        .addComponent(ErrorMessageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 456, Short.MAX_VALUE))
                    .addGroup(ButtonPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(SearchTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 367, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
                        .addComponent(DetailsButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        ButtonPanelLayout.setVerticalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, ButtonPanelLayout.createSequentialGroup()
                .addComponent(ErrorMessageLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DetailsButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SearchTitle))
                .addContainerGap())
        );

        InstructionsLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        InstructionsLabel.setText("Please select an item and click button to take action");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Image", "Name", "Make", "Model"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setRowHeight(96);
        jTable1.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(TopPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(CategoryPane, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(ButtonPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(InstructionsLabel, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 555, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TopPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(CategoryPane, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(InstructionsLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ButtonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    /**
     * 
     * @param evt
     */
    private void DetailsButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DetailsButton1ActionPerformed
        Item selection = null;
        int row = jTable1.getSelectedRow();
        if (row >= 0) {
            selection = cart.getSearchResults().get(row);
            desktopControllerHelper.ItemDetailsScreen(selection);
        }

    }//GEN-LAST:event_DetailsButton1ActionPerformed

    /**
     * 
     * @param evt
     */
    private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GoButtonActionPerformed
        String keyWord = SearchField.getText();
        String criteria = OptionSearch.getModel().getSelectedItem().toString();
        if ("".equalsIgnoreCase(keyWord.trim())) {
            ErrorMessageLabel.setText("Please enter a keyword");
            ErrorMessageLabel.setVisible(true);
        } else {
            desktopController.searchByKeyword(keyWord, criteria);
            if (sessionManager.getCart().getSearchResults().size() == 0) {
                ErrorMessageLabel.setText("No item found by that description.");
                ErrorMessageLabel.setVisible(true);
            }
        }
        updateModel();
    }//GEN-LAST:event_GoButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel ButtonPanel;
    private javax.swing.JLabel ByLabel;
    private javax.swing.JScrollPane CategoryPane;
    private javax.swing.JTree CategoryTree;
    private javax.swing.JButton DetailsButton1;
    private javax.swing.JLabel ErrorMessageLabel;
    private javax.swing.JButton GoButton;
    private javax.swing.JLabel InstructionsLabel;
    private javax.swing.JComboBox OptionSearch;
    private javax.swing.JTextField SearchField;
    private javax.swing.JLabel SearchLabel;
    private javax.swing.JLabel SearchTitle;
    private javax.swing.JPanel TopPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
